import { nanoid } from "nanoid"
import Mock from 'mockjs'

interface AllPersonType {
    [propName: string]: any
}

let AllPerson: AllPersonType = [{ username: "2", password: 2, token: "sadfsadfTom", isLogin: true, role: "管理" },
{ username: "3", password: 3, token: "aadfafJerry", isLogin: true, role: "用户" }
]//{ username: "tom", password: 123, token: nanoid() + "tom", isLogin: true }

export const registered = (config: any) => {
    let { username, password } = JSON.parse(config.body)
    AllPerson.push({ username: username, password: password, token: nanoid() + username, isLogin: true })
    return {
        code: 200,
        msg: "Yes",
        data: null
    }
}

export const loginApi = (config: any) => {

    const { username, password } = JSON.parse(config.body)
    const current = AllPerson.filter((item: any) => {
        return item.username == username && item.password == password
    })
    return {
        code: 200,
        msg: "Yes",
        data: current[0] ? current[0] : null
    }
}

export const getUserRole = (config: any) => {
    let token = JSON.parse(config.body).token
    let user = AllPerson.find((item: any) => {

        return item.token == token
    })

    return {
        code: 200,
        msg: "Yes",
        data: user
    }
}


interface MockObj {
    id: string,
    code: string,
    drugname: string,
    num: string,
    boxx: string,
    price: string,
    [key: string]: string
}
let List: MockObj[] = []

// 添加药品
export const saijieApi = (config: any) => {
    const { code, drugname, num, boxx } = JSON.parse(config.body)
    var id = Mock.mock('@guid')
    // List.unshift({
    //     id: id,
    //     code: code,
    //     drugname: drugname,
    //     num: num,
    //     boxx: boxx,
    //     price: Mock.mock({
    //         "number|1-100.1-10": 1
    //     }),
    // })
    let obj = {
        id: id, code: code, drugname: drugname, num: num, boxx: boxx, price: Mock.mock({"number|1-100.1-10": 1})
    }
    return {
        code: 200,
        msg: "Yes",
        data: obj
    }
}